﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Nuzzle.Managers
{
    public static class ChatManager
    {
        public static void ChatMessage(Network.GameClient client, string content, byte channel)
        {
            var channelType = (Protocol.Enums.ChatChannelsMultiEnum)channel;
            content = content.Replace("<", "&lt;").Replace(">", "&gt;");

            if (content.Length > 140)
                return;

            switch(channelType)
            {
                case Protocol.Enums.ChatChannelsMultiEnum.CHANNEL_GLOBAL:
                    if (content.StartsWith("."))//Commande joueur
                    {
                        var parameters = content.Substring(1).Split(' ').ToList();
                        var header = parameters[0];
                        parameters.RemoveAt(0);
                        PlayerCommand(client, header, parameters);
                    }
                    else
                    {
                        client.Character.Map.Engine.Send(new Protocol.Messages.Game.Chat.ChatServerMessage
                            (client.Character.ID, client.Character.Nickname, client.Account.ID,
                            channel, content, 0, ""));
                    }
                    break;

                case Protocol.Enums.ChatChannelsMultiEnum.CHANNEL_SEEK:
                    World.Send(new Protocol.Messages.Game.Chat.ChatServerMessage
                            (client.Character.ID, client.Character.Nickname, client.Account.ID,
                            channel, content, 0, ""));
                    break;

                case Protocol.Enums.ChatChannelsMultiEnum.CHANNEL_SALES:
                    World.Send(new Protocol.Messages.Game.Chat.ChatServerMessage
                            (client.Character.ID, client.Character.Nickname, client.Account.ID,
                            channel, content, 0, ""));
                    break;
            }
        }

        public static void PlayerCommand(Network.GameClient client, string header, List<string> data)
        {
            switch (header)
            {
                case "start":
                    client.Teleport(Utilities.Settings.AppSettings.GetIntElement("Gameplay.StartMap"),
                        Utilities.Settings.AppSettings.GetIntElement("Gameplay.StartCell"));
                    client.SystemMessage("Téléporter a la carte de départ !");
                    break;

                case "infos":
                    var infos = new StringBuilder();
                    infos.Append("<b>Nuzzle</b> v" + Utilities.Basic.GetNuzzleVersion() + " by <b>NightWolf</b><br />");
                    infos.Append("Joueurs en ligne : <b>" + World.GetGameClients().Length + "</b>");
                    client.SystemMessage(infos.ToString());
                    break;
            }
        }
    }
}
